package com.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.myblog.model.po.SysPermission;
import com.myblog.mapper.SysPermissionMapper;
import com.myblog.service.SysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-03
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {


    @Override
    public Boolean deleteById(Long id) {
        //删除当前id全县
        getBaseMapper().deleteById(id);
        //删除子权限
        LambdaQueryWrapper<SysPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPermission::getParentId,id);
        getBaseMapper().delete(queryWrapper);
        return true;
    }

    @Override
    public List<SysPermission> findByUserId(Long userId) {
        List<SysPermission> sysPermissions = this.baseMapper.selectPermissionByUserId(userId);
        if(CollectionUtils.isEmpty(sysPermissions)){
            //把空的Null对象移除
            sysPermissions.remove(null);
        }
        return sysPermissions;
    }
}
